<?
if($data['date']){
	$params['payed_reseller'] = substr($data['date'],0,10);
}else{
	$params['payed_reseller'] = array('isnull');
	if($data['period']){
		$params['order_date'] = $data['period'];
	}
}
$params['reseller_id'] = $data['reseller_id'];
$params['order_status'] = array(1,2,4,8,16,32,64,1024);

$options['group_by'] = array('order_id','order_status','reseller','username','sale_type','product_type','payment_currency_','client_blocked','order_date','payed_reseller','payed_referer');
$options['order_by'] = 'order_id';
$options['order_by_'] = 'DESC';
$options['limit'] = 100000;

$result['sales'] = list_sales($params,$options);

$query = "SELECT reseller_id FROM resellers WHERE referer_id=$params[reseller_id]";
$master_db->query($query);
if($master_db->numRows()>0){
	$tmp = $master_db->getAll();
	$tmp = array_values($tmp);
	foreach($tmp as $k=>$v){
		$params1['reseller_id'][] = $v['reseller_id'];
	}
	$params1['payed_referer'] = $params['payed_reseller'];
	$params1['order_status'] = $params['order_status'];
	if($params['order_date']){
		$params1['order_date'] = $params['order_date'];
	}

	$result['affiliates'] = list_sales($params1,$options,'struct','affiliates');
}

$bitmap = array(
	'client_blocked'=>array(0=>'Active',1=>'Blocked',2=>'Paused',3=>'Expired',4=>'Active'),
	'currencies'=>array('USD'=>'US$','CAD'=>'CA$','AUD'=>'AU$','EUR'=>'&#8364','GBP'=>'&pound'),
	'order_status'=>array(0=>'Not Verified',128=>'Not Verified',256=>'Not Verified',512=>'Not Verified',16=>'OK',32=>'OK',2=>'Fraud',1=>'Refund',4=>'MBG',8=>'Charge-Back',64=>'Charge-Back',1024=>'Refund')
);
$order_ids = array();

        if($data['date']){
                $t_tbl_content = get_reseller_commission_($params[reseller_id],substr($data['date'],0,10));
                $t_tbl_content = current($t_tbl_content);
        }else{
                $t_tbl_content = get_reseller_commission_($params[reseller_id]);
                $t_tbl_content = $t_tbl_content[($data['period'])];
        }

        $t_tbl = "
                <table class='tbl' cellspacing=0 cellpadding=0>
                        <tr>
                                <th align='left' style='border-right: 1px solid #FFFFFF;padding-left:15px;'>Description</th>
                                <th style='border-right: 1px solid #FFFFFF;padding-left:15px;padding-right:15px;'>Count</th>
                                <th>Amount</th>
                        </tr>
                        <tr>
                                <td><a href='#sales'>Your Sales</a></td>
                                <td align=center>$t_tbl_content[count_sales]</td>
                                <td align='right'>".$bitmap['currencies']['USD']." ".number_format($t_tbl_content[amount_sales],2,'.',',')."</td>
                        </tr>
                        <tr style='background-color:#EBEBEC;'>
                                <td><a href='#affiliates'>Affiliate Sales</a></td>
                                <td align=center>$t_tbl_content[count_affiliates]</td>
                                <td align='right'>".$bitmap['currencies']['USD']." ".number_format($t_tbl_content[amount_affiliates],2,'.',',')."</td>
                        </tr>
                        <tr>
                                <td>Net Profit</td>
                                <td align=center>$t_tbl_content[count]</td>
                                <td align='right'>".$bitmap['currencies']['USD']." ".number_format($t_tbl_content[amount],2,'.',',')."</td>
                        </tr>
                </table>
        ";


if($data['date']){
	$payment_ = get_payment_history__($data['reseller_id'],substr($data['date'],0,10));
	switch ($payment_[0]['payment_method']){
		case 'check' :
			if($payment_['0'][status]=='none'){
				$tmp = 'sent ['.date('m/d/Y',strtotime($payment_['0']['change_status_date'])).']';
			}else{
				$tmp = $payment_['0']['status'].' ['.date('m/d/Y',strtotime($payment_['0']['change_status_date'])).']';
			}
			$p_table = "<table class='tbl' cellspacing=0 cellpadding=0>
				<tr><th style='border-right: 1px solid #FFFFFF;'>Check Payment</th><th style='border-right: 1px solid #FFFFFF;'>Status</th><th>Recepient's Address</th></tr>
				<tr>
					<td align=center>Check [#".$payment_['0']['check_number']."]</td>
					<td align=center>$tmp</td>
					<td align=center>
						".ucwords($payment_['0']['first_name'].' '.$payment_['0']['last_name'])."<br>
						".($payment_['0']['address2'] ? ucwords($payment_['0']['address1'].', '.$payment_['0']['address2']) : ucwords($payment_['0']['address1']))."<br>
						".(ucwords($payment_['0']['city']).', '.strtoupper($payment_['0']['state'].' '.$payment_['0']['zip']))."<br>
						".$payment_['0']['country_long']."
					</td>
				</tr>
			</table>";
		break;
		case 'paypal' :
	                if($payment_['0'][status]=='none'){
        	                $tmp = 'upcoming ['.date('m/d/Y',strtotime($payment_['0']['change_status_date'])).']';
	                }else{
        	                $tmp = $payment_['0']['status'].' ['.date('m/d/Y',strtotime($payment_['0']['change_status_date'])).']';
	                }
			$p_table = "<table class='tbl' cellspacing=0 cellpadding=0>
				<tr><th style='border-right: 1px solid #FFFFFF;'>PayPal Payment</th><th style='border-right: 1px solid #FFFFFF;'>Status</th><th>PayPal Email</th></tr>
				<tr>
					<td align=center>PayPal".($payment_['0']['paypal_transaction_id'] ? " [ID ".$payment_['0']['paypal_transaction_id']."]" : "")."</td>
					<td align=center>$tmp</td>
					<td align=center>".$payment_['0']['paypal_email']."</td>
				</tr>
			</table>";
		break;
		case 'wire' :
			if($payment_['0'][status]=='none'){
				$tmp = 'upcoming ['.date('m/d/Y',strtotime($payment_['0']['change_status_date'])).']';
			}else{
				$tmp = $payment_['0']['status'].' ['.date('m/d/Y',strtotime($payment_['0']['change_status_date'])).']';
			}
			$p_table = "<table class='tbl' cellspacing=0 cellpadding=0>
				<tr><th style='border-right: 1px solid #FFFFFF;'>Wire Payment</th><th style='border-right: 1px solid #FFFFFF;'>Status</th><th>Bank Details</th></tr>
				<tr>
					<td align=center>Wire [#".$payment_['0']['payment_id']."]</td>
					<td align=center>$tmp</td>
					<td align=center>
						<b>Swift Code : </b>".$payment_['0'][swift_code]."<br>
						<b>Bank Name : </b>".$payment_['0'][bank_name]."<br>
						<b>Bank Address : </b>".$payment_['0'][bank_address]."<br>
						<b>Account Number : </b>".$payment_['0'][account_number]."<br>
						<b>Routing Number : </b>".$payment_['0'][routing_number]."<br>
						<b>Account Holder : </b>".$payment_['0'][account_holder]."
					</td>
				</tr>
			</table>";
		break;
		case 'wallet' :
                        if($payment_['0'][status]=='none'){
                                $tmp = 'upcoming ['.date('m/d/Y',strtotime($payment_['0']['change_status_date'])).']';
                        }else{
                                $tmp = $payment_['0']['status'].' ['.date('m/d/Y',strtotime($payment_['0']['change_status_date'])).']';
                        }
			$p_table = "<table class='tbl' cellspacing=0 cellpadding=0>
				<tr><th style='border-right: 1px solid #FFFFFF;'>Wallet Payment</th><th>Status</th></tr>
				<tr><td align=center>Wallet [#".$payment_['0']['payment_id']."]</td><td align=center>$tmp</td></tr>
			</table>";
		break;
	}
}
//##############################################################################
//#################       Sales         ########################################
//##############################################################################

if($result['sales']['num_rows']>0){
	$result_table1 = '<br><table class="master_tbl" width="1024"  border="0" cellspacing="0" cellpadding="0" id="sales">
		<tr>
			<td class="black" style="padding-bottom: 3px; font-size: 10px;" colspan=8><img src="sales/images/arrow_red.jpg"><strong> Your Own Personal Sales for the Period : </strong><span class="silver">[ from: </span><span class="red">'.date('M d, Y',strtotime($result['sales']['result'][(count($result['sales']['result'])-1)]['order_date'])).'</span><span class="silver"> to: </span><span class="red">'.date('M d, Y',strtotime($result['sales']['result']['0']['order_date'])).'</span><span class=silver>]</span></td><td align="right"><a href="#" onClick="window.open(\'sales/payment_sales_help.html#reseller_sales\',\'\',\'height=600,width=800,scrollbars=yes\');return false;"><img src="sales/images/help.gif" alt="Online Help" border="0"></a></td>
		</tr>
		<tr><td colspan="9" height="2" bgcolor="#696969"></td></tr>
		<tr bgcolor="#DDE5E8">
		<td align="center" class="black"><strong>OrderID</strong></td>
		<td align="center" class="black"><strong>Date</strong><br/>mm/dd/yyyy</td>
		<td align="center" class="black"><strong>Username</strong></td>
		<td align="center" class="black"><strong>Type</strong></td>
		<td align="center" class="black"><strong>Item</strong></td>
		<td align="center">
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
			  <tr>
				<td nowrap style="text-align:center;border-right:0px;" bgcolor="#C6D5DC" colspan="3" class="black"><strong>Transaction</strong></td>
			  </tr>
			  <tr bgcolor="#DDE5E8">
				<td width=80 style="text-align:center;border-bottom:0px;border-right:0px;" class="black"><strong>Total</strong></td>
				<td width=80 style="text-align:center;border-bottom:0px;border-right:0px;" class="black"><strong>Wholesale</strong></td>
				<td width=60 style="text-align:center;border-bottom:0px;border-right:0px;" class="black"><strong>Status</strong></td>
			  </tr>
			</table>
		</td>
		<td align="center" class="black"><strong>Account<br/>Status</strong></td>
		<td align="center" class="black"><strong>Net<br/>Profit</strong></td>
		<td align="center" class="black"><strong>Issued</strong><br/>mm/dd/yyyy</td>
		</tr><tr><td colspan="9" height="2" bgcolor="#696969"></td></tr>';
	foreach($result['sales']['result'] as $k => $row){
		if($totals1[($row[payment_currency_])]){
			$totals1[($row[payment_currency_])]['amount_total'] = bcadd($totals1[($row[payment_currency_])]['amount_total'],$row['amount_total'],2);
			$totals1[($row[payment_currency_])]['amount_wholesale'] = bcadd($totals1[($row[payment_currency_])]['amount_wholesale'],$row['amount_wholesale'],2);
			$totals1[($row[payment_currency_])]['amount_reseller'] = bcadd($totals1[($row[payment_currency_])]['amount_reseller'],$row['amount_reseller'],2);
			if(!in_array($row[order_id],$order_ids)){
				$totals1[($row[payment_currency_])]['count'] += 1;
				$order_ids[] = $row[order_id];
			}
		}else{
			$totals1[($row[payment_currency_])]['amount_total'] = $row['amount_total'];
			$totals1[($row[payment_currency_])]['amount_wholesale'] = $row['amount_wholesale'];
			$totals1[($row[payment_currency_])]['amount_reseller'] = $row['amount_reseller'];
			if(!in_array($row[order_id],$order_ids)){
				$totals1[($row[payment_currency_])]['count'] = 1;
				$order_ids[] = $row[order_id];
			}
		}

	if(in_array($row['order_status'],array(1,2,4,1024))){
		$style = 'bgcolor=#FDC7F9';
		if($row['sale_type']=='reversal'){
			$style = 'bgcolor=#A15C9C';
		}
	}elseif(in_array($row['order_status'],array(8,64))){
		$style = 'bgcolor=#FECA50';
	}else{
		if(in_array($row['client_blocked'],array(1,3))){
			$style = 'bgcolor=#9EDAFC';
		}else{
			if($flag){
				if($tmp_order_id != $row[order_id]){
					$tmp_order_id = $row[order_id];
					unset($flag);
					$style = 'bgcolor=#EBEBEB';
				}
			}else{
                                if($tmp_order_id != $row[order_id]){
					$tmp_order_id = $row[order_id];
					$flag = 1;
					$style = 'bgcolor=#FFFFFF';
				}
			}
		}
	}
		$result_table1 .= "<tr $style>
		<td nowrap style='padding-left:5px;' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='orange2'>$row[order_id]</td>
		<td nowrap height='16' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black' align='center'>".date('m/d/Y',strtotime($row[order_date]))."</td>
		<td nowrap style='padding-left:10px;' ".($style == 'bgcolor=#A15C9C' ?  "class='orange'" : "")." class='orange2'>".($row[username] ? $row[username] : '-')."</td>
		<td nowrap align='center' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>$row[sale_type]</td>
		<td nowrap align='center' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>".($row[product_type] ? $row[product_type] : '-')."</td>
		<td>
			<table  width='100%' border='0' cellspacing='0' cellpadding='0'>
				<tr>
					<td width=75 align=right style='padding-right:5px;border-bottom:0px;border-right:0px;' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>".($bitmap[currencies][($row[payment_currency_])])." $row[amount_total]</td>
					<td width=75 align=right style='padding-right:5px;border-bottom:0px;border-right:0px;' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>".($bitmap[currencies][($row[payment_currency_])])." $row[amount_wholesale]</td>
					<td width=55 align=center style='padding-left:5px;border-bottom:0px;border-right:0px;' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>".($row[sale_type]!='reversal' ? $bitmap['order_status'][($row[order_status])] : ($row[order_status]=='1024' ? 'P.Refund' : 'F.Refund'))."</td>
				</tr>
			</table>
		</td>
		<td nowrap align='center' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>".(is_numeric($row[client_blocked]) ? $bitmap['client_blocked'][($row[client_blocked])] : '-')."</td>
		<td nowrap style='padding-right:5px;' align='right' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>".$bitmap[currencies]['USD']." $row[amount_reseller]</td>
		<td nowrap align='center' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>".($data[date] ? date('m/d/Y',strtotime($data[date])) : '-')."</td>
		</tr>";
	}

	$result_table1 .= '</tr><tr><td colspan="9" height="2" bgcolor="#696969"></td></tr>';

	foreach($totals1 as $currency=>$row){
		$result_table1 .= "<tr bgcolor='#0660B6'>
			<td height=16 class='white' style='padding-left:10px;border-bottom:0px;border-right:0px;'>$row[count]</td>
			<td colspan=4 class='white' style='border-bottom:0px;border-right:0px;'>SUB-TOTALS:</td>
			<td style='border-bottom:0px;border-right:0px;'>
				<table width='100%' border='0' cellspacing='0' cellpadding='0'>
					<tr>
						<td width='75' align=right style='padding-right:5px;border-bottom:0px;border-right:0px;' class='white'>".$bitmap[currencies][$currency]." ".number_format($row[amount_total], 2, '.', ',')."</td>
						<td width='75' align=right style='padding-right:5px;border-bottom:0px;border-right:0px;' class='white'>".$bitmap[currencies][$currency]." ".number_format($row[amount_wholesale], 2, '.', ',')."</td>
						<td width='60' style='border-bottom:0px;border-right:0px;' class='white'></td>
					</tr>
				</table>
			</td>
			<td style='border-bottom:0px;border-right:0px;' class='white'></td>
			<td align=right style='padding-right:5px;border-bottom:0px;border-right:0px;' class='white'>".$bitmap[currencies][USD]." ".number_format($row[amount_reseller], 2, '.', ',')."</td>
			<td style='border-bottom:0px;' class='white'></td>
		</tr><tr><td height='1' colspan='9' bgcolor='#EBEBEB'></td></tr>";
	}

	$result_table1 .= '</tr><tr><td colspan="9" height="2" bgcolor="#696969"></td></tr></table><br>';
}

//##############################################################################
//#################       Affiliates    ########################################
//##############################################################################

if($result['affiliates']['num_rows']>0){
	$result_table2 = '<br><table class="master_tbl" width="1024"  border="0" cellspacing="0" cellpadding="0" id="affiliates">
		<tr><td class="black" style="padding-bottom: 3px; font-size: 10px;" colspan=9><img src="sales/images/arrow_red.jpg"><strong> Affiliate Sales for the Period: </strong><span class="silver">[ from: </span><span class="red">'.date('M d, Y',strtotime($result['affiliates']['result'][(count($result['affiliates']['result'])-1)]['order_date'])).'</span><span class="silver"> to: </span><span class="red">'.date('M d, Y',strtotime($result['affiliates']['result']['0']['order_date'])).'</span><span class="silver">]</span></td><td align="right"><a href="#" onClick="window.open(\'sales/payment_sales_help.html#affiliate_sales\',\'\',\'height=600,width=800,scrollbars=yes\');return false;"><img src="sales/images/help.gif" alt="Online Help" border="0"></a></td></tr>
		<tr><td colspan="10" height="2" bgcolor="#696969"></td></tr>
		<tr bgcolor="#DDE5E8">
		<td align="center" class="black"><strong>OrderID</strong></td>
		<td align="center" class="black"><strong>Date</strong><br/>mm/dd/yyyy</td>
		<td align="center" class="black"><strong>Affiliate</strong></td>
		<td align="center" class="black"><strong>Username</strong></td>
		<td align="center" class="black"><strong>Type</strong></td>
		<td align="center" class="black"><strong>Item</strong></td>
		<td align="center">
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
			  <tr>
				<td style="text-align:center;border-right:0px;" bgcolor="#C6D5DC" colspan="3" class="black"><strong>Transaction</strong></td>
			  </tr>
			  <tr bgcolor="#DDE5E8">
				<td width=80 style="text-align:center;border-bottom:0px;border-right:0px;" class="black"><strong>Total</strong></td>
				<td width=80 style="text-align:center;border-bottom:0px;border-right:0px;" class="black"><strong>Wholesale</strong></td>
				<td width=60 style="text-align:center;border-bottom:0px;border-right:0px;" class="black"><strong>Status</strong></td>
			  </tr>
			</table>
		</td>
		<td align="center" class="black"><strong>Account<br/>Status</strong></td>
		<td align="center" class="black"><strong>Net<br/>Profit</strong></td>
		<td align="center" class="black"><strong>Issued</strong><br/>mm/dd/yyyy</td>
		</tr><tr><td colspan="10" height="2" bgcolor="#696969"></td></tr>';
	foreach($result['affiliates']['result'] as $k => $row){
		if($totals2[($row[payment_currency_])]){
			$totals2[($row[payment_currency_])]['amount_total'] = bcadd($totals2[($row[payment_currency_])]['amount_total'],$row['amount_total'],2);
			$totals2[($row[payment_currency_])]['amount_wholesale'] = bcadd($totals2[($row[payment_currency_])]['amount_wholesale'],$row['amount_wholesale'],2);
			$totals2[($row[payment_currency_])]['amount_referer'] = bcadd($totals2[($row[payment_currency_])]['amount_referer'],$row['amount_referer'],2);
			if(!in_array($row[order_id],$order_ids)){
				$totals2[($row[payment_currency_])]['count'] += 1;
				$order_ids[] = $row[order_id];
			}
		}else{
			$totals2[($row[payment_currency_])]['amount_total'] = $row['amount_total'];
			$totals2[($row[payment_currency_])]['amount_wholesale'] = $row['amount_wholesale'];
			$totals2[($row[payment_currency_])]['amount_referer'] = $row['amount_referer'];
			if(!in_array($row[order_id],$order_ids)){
				$totals2[($row[payment_currency_])]['count'] = 1;
				$order_ids[] = $row[order_id];
			}
		}

	if(in_array($row['order_status'],array(1,2,4,1024))){
		$style = 'bgcolor=#FDC7F9';
                if($row['sale_type']=='reversal'){
			$style = 'bgcolor=#A15C9C';
		}
	}elseif(in_array($row['order_status'],array(8,64))){
		$style = 'bgcolor=#FECA50';
	}else{
		if(in_array($row['client_blocked'],array(1,3))){
			$style = 'bgcolor=#9EDAFC';
		}else{
			if($flag){
                                if($tmp_order_id != $row[order_id]){
                                        $tmp_order_id = $row[order_id];
					unset($flag);
					$style = 'bgcolor=#EBEBEB';
				}
			}else{
                                if($tmp_order_id != $row[order_id]){
                                        $tmp_order_id = $row[order_id];
					$flag = 1;
					$style = 'bgcolor=#FFFFFF';
				}
			}
		}
	}
		$result_table2 .= "<tr $style>
		<td nowrap style='padding-left:5px;' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='orange2'>$row[order_id]</td>
		<td nowrap height='16' align='center' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>".date('m/d/Y',strtotime($row[order_date]))."</td>
		<td nowrap style='padding-left:10px;' ".($style == 'bgcolor=#A15C9C' ?  "class='white_blue'" : "")." class='blue'>$row[reseller]</td>
		<td nowrap style='padding-left:10px;' ".($style == 'bgcolor=#A15C9C' ?  "class='orange'" : "")." class='orange2'>".($row[username] ? $row[username] : '-')."</td>
		<td nowrap align='center' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>$row[sale_type]</td>
		<td nowrap align='center' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>".($row[product_type] ? $row[product_type] : '-')."</td>
		<td>
			<table  width='100%' border='0' cellspacing='0' cellpadding='0'>
				<tr>
					<td nowrap width=75 align=right style='padding-right:5px;border-bottom:0px;border-right:0px;' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>".($bitmap[currencies][($row[payment_currency_])])." $row[amount_total]</td>
					<td nowrap width=75 align=right style='padding-right:5px;border-bottom:0px;border-right:0px;' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>".($bitmap[currencies][($row[payment_currency_])])." $row[amount_wholesale]</td>
					<td nowrap width=55 align=center style='padding-left:5px;border-bottom:0px;border-right:0px;' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>".($row[sale_type]!='reversal' ? $bitmap['order_status'][($row[order_status])] : ($row[order_status]=='1024' ? 'P.Refund' : 'F.Refund'))."</td>
				</tr>
			</table>
		</td>
		<td nowrap align='center' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>".(is_numeric($row[client_blocked]) ? $bitmap['client_blocked'][($row[client_blocked])] : '-')."</td>
		<td nowrap style='padding-right:5px;' align='right' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>".$bitmap[currencies]['USD']." $row[amount_referer]</td>
		<td nowrap align='center' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>".($data[date] ? date('m/d/Y',strtotime($data[date])) : '-')."</td>
		</tr>";
	}

	$result_table2 .= '</tr><tr><td height="2" colspan="10" bgcolor="#696969"></td></tr>';
	foreach($totals2 as $currency=>$row){
		$result_table2 .= "<tr bgcolor='#0660B6'>
			<td height=16 class='white' style='padding-left:10px;border-bottom:0px;border-right:0px;'>$row[count]</td>
			<td colspan=5 class='white' style='border-bottom:0px;border-right:0px;'>SUB-TOTALS:</td>
			<td style='border-bottom:0px;border-right:0px;'>
				<table width='100%' border='0' cellspacing='0' cellpadding='0'>
					<tr>
						<td width='75' align=right style='padding-right:5px;border-bottom:0px;border-right:0px;' class='white'>".$bitmap[currencies][$currency]." ".number_format($row[amount_total], 2, '.', ',')."</td>
						<td width='75' align=right style='padding-right:5px;border-bottom:0px;border-right:0px;' class='white'>".$bitmap[currencies][$currency]." ".number_format($row[amount_wholesale], 2, '.', ',')."</td>
						<td width='60' style='border-bottom:0px;border-right:0px;' class='white'></td>
					</tr>
				</table>
			</td>
			<td style='border-bottom:0px;border-right:0px;' class='white'></td>
			<td align=right style='padding-right:5px;border-bottom:0px;border-right:0px;' class='white'>".$bitmap[currencies][USD]." ".number_format($row[amount_referer], 2, '.', ',')."</td>
			<td style='border-bottom:0px;' class='white'></td>
			</tr><tr><td height='1' colspan='10' bgcolor='#EBEBEB'></td></tr>";
	}
	$result_table2 .= '</tr><tr><td height="2" colspan="10" bgcolor="#696969"></td></tr></table><br>';
}

//##############################################################################
//#################      Cpanel Affiliates         #############################
//##############################################################################
if($t_tbl_content['count_cpanel_affiliates']>0){
	$tbl3 = get_cpanel_affiliate_sales($data['reseller_id'],$data['period'],$data['date']);
	$result_table3 = '<br><table class="master_tbl" width="1024"  border="0" cellspacing="0" cellpadding="0" id="affiliates">
			<tr><td class="black" style="padding-bottom: 3px; font-size: 10px;" colspan=8><img src="sales/images/arrow_red.jpg"><strong> Cpanel Affiliate Sales for the Period: </strong><span class="silver">[ from: </span><span class="red">'.date('M d, Y',strtotime($tbl3[(count($tbl3)-1)]['date'])).'</span><span class="silver"> to: </span><span class="red">'.date('M d, Y',strtotime($tbl3['0']['date'])).'</span><span class="silver">]</span></td><td align="right"><a href="#" onClick="window.open(\'sales/payment_sales_help.html#cpanel_affiliates\',\'\',\'height=600,width=800,scrollbars=yes\');return false;"><img src="sales/images/help.gif" alt="Online Help" border="0"></a></td></tr>
			<tr><td colspan="9" height="2" bgcolor="#696969"></td></tr>
			<tr bgcolor="#DDE5E8">
			<td align="center" class="black"><strong>InvoiceID</strong></td>
			<td align="center" class="black"><strong>Date</strong><br/>mm/dd/yyyy</td>
			<td align="center" class="black"><strong>Affiliate</strong></td>
			<td align="center" class="black"><strong>Type</strong></td>
			<td align="center" class="black"><strong>Item</strong></td>
			<td align="center" class="black"><strong>Total</strong></td>
			<td align="center" class="black"><strong>Wholesale</strong></td>
			<td align="center" class="black"><strong>Net<br/>Profit</strong></td>
			<td align="center" class="black"><strong>Issued</strong><br/>mm/dd/yyyy</td>
			</tr><tr><td colspan="9" height="2" bgcolor="#696969"></td></tr>';
		foreach($tbl3 as $k=>$row){
			if($totals3[($row[payment_currency])]){
				$totals3[($row[payment_currency])][amount] = bcadd($totals3[($row[payment_currency])][amount],$row[amount],2);
				$totals3[($row[payment_currency])][amount_cpanel_affiliate] = bcadd($totals3[($row[payment_currency])][amount_cpanel_affiliate],$row[amount_cpanel_affiliate],2);
				$totals3[($row[payment_currency])][total_price] = bcadd($totals3[($row[payment_currency])][total_price],$row[total_price],2);
				$totals3[($row[payment_currency])][count] = $totals3[($row[payment_currency])][count]+1;
			}else{
				$totals3[($row[payment_currency])][amount] = $row[amount];
				$totals3[($row[payment_currency])][amount_cpanel_affiliate] = $row[amount_cpanel_affiliate];
				$totals3[($row[payment_currency])][total_price] = $row[total_price];
				$totals3[($row[payment_currency])][count] = 1;
			}

			if($style == 'bgcolor=#EBEBEB'){
				$style = 'bgcolor=#FFFFFF';
			}else{
				$style = 'bgcolor=#EBEBEB';
			}

			$result_table3 .= "<tr $style>
			<td nowrap style='padding-left:5px;' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='orange2'>$row[invoice_id]</td>
			<td nowrap height='16' align='center' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>".date('m/d/Y',strtotime($row[date]))."</td>
			<td nowrap style='padding-left:10px;' ".($style == 'bgcolor=#A15C9C' ?  "class='white_blue'" : "")." class='blue'>$row[affiliate]</td>
			<td nowrap align='center' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>$row[sale_type]</td>
			<td nowrap align='center' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>$row[plan_name]</td>
			<td nowrap align='center' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>".($bitmap[currencies][($row[payment_currency])])." $row[total_price]</td>
			<td nowrap align='center' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>".($bitmap[currencies][($row[payment_currency])])." $row[amount]</td>
			<td nowrap align='center' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>".($bitmap[currencies][($row[payment_currency])])." $row[amount_cpanel_affiliate]</td>
			<td nowrap align='center' ".($style == 'bgcolor=#A15C9C' ?  "class='white-normal'" : "")." class='black'>".($data[date] ? date('m/d/Y',strtotime($data[date])) : '-')."</td>
			</tr>";
		}

		foreach($totals3 as $currency=>$row){
			$result_table3 .= "<tr bgcolor='#0660B6'>
				<td height=16 class='white' style='padding-left:10px;border-bottom:0px;border-right:0px;'>$row[count]</td>
				<td colspan=4 class='white' style='border-bottom:0px;border-right:0px;'>SUB-TOTALS:</td>
				<td align=center style='padding-right:5px;border-bottom:0px;border-right:0px;' class='white'>".$bitmap[currencies][USD]." ".number_format($row[total_price], 2, '.', ',')."</td>
				<td align=center style='padding-right:5px;border-bottom:0px;border-right:0px;' class='white'>".$bitmap[currencies][USD]." ".number_format($row[amount], 2, '.', ',')."</td>
				<td align=center style='padding-right:5px;border-bottom:0px;border-right:0px;' class='white'>".$bitmap[currencies][USD]." ".number_format($row[amount_cpanel_affiliate], 2, '.', ',')."</td>
				<td style='border-bottom:0px;' class='white'></td>
				</tr><tr><td height='1' colspan='10' bgcolor='#EBEBEB'></td></tr>";
		}
		$result_table3 .= '</tr><tr><td height="2" colspan="10" bgcolor="#696969"></td></tr></table><br>';
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Payments Sales</title>
<link href="sales/styles.css" rel="stylesheet" type="text/css">
<script language="javascript" src="sales/tooltip.js"></script>

<?

echo "<table cellspacing=0 cellpadding=0>
		<tr>
			<td colspan=2 class='black' style='padding-bottom: 3px; font-size: 10px;'><img src='sales/images/arrow_red.jpg'><strong> Sales & Payment Details : </td>
			<td align=right style='padding-bottom: 3px;'><a href='#' onClick=\"window.open('sales/payment_sales_help.html#top_tables','','height=600,width=800,scrollbars=yes');return false;\"><img src='sales/images/help.gif' alt='Online Help' border='0'></a></td></tr>
		<tr><td valign=top>$t_tbl</td><td width=20></td><td valign=top>$p_table</td></tr>
</table>";
echo $result_table1;
echo $result_table2;
echo $result_table3;
?>
